package com.woniu.controller.operation;

import com.alipay.api.AlipayApiException;
import com.woniu.controller.BaseController;
import com.woniu.entity.Result;
import com.woniu.mapper.OrderMapper;
import com.woniu.pojo.Order;
import com.woniu.utils.AliPayUtil;
import com.woniu.vo.OrderSeachByIdReturnVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
@Api(tags = "模拟支付")
@Controller
@RequestMapping("/aliPay")
public class AliPayController extends BaseController {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private AliPayUtil aliPayUtil;
    @RequestMapping("/{orderId}")
    @ApiOperation("模拟订单支付")
    public String pay(@PathVariable Long orderId, Model model) throws AlipayApiException {
        //我先根据传过来的订单id去查订单
        Order order = orderMapper.selectById(orderId);
        //非空校验一下吧
        if(order == null){
            return null;
        }
        //因为订单支付需要三个参数，一个是订单标号，一个是订单金额，一个是订单的title
        //先拿到订单的金额
        BigDecimal money = order.getMoney();
        //因为我想要的是把订单的title组装成  用户的名字+的订单，所以就查一下用户的名字
        OrderSeachByIdReturnVo orderSeachByIdReturnVo = orderMapper.selectDetailsById(orderId);
        String userName = orderSeachByIdReturnVo.getUserName();
        String title = userName+"的订单";
        //这个工具类要的三个参数都是字符串类型的，强转一下  （支付）
        String pay = aliPayUtil.pay(orderId.toString(), money.toString(), title);
        //它的返回值是个页面，付款页面，先用thymeleaf做个效果
        model.addAttribute("form",pay);
        return "pay";
    }


}
